<script lang="ts" setup name="XtxCarousel">
import { BannerItem } from '@/types/data'
import { onMounted, onUnmounted, ref } from 'vue'

// 1. js 的方式
// defineProps({
//   slides: {
//     type: Array as PropType<BannerItem[]>,
//     required: true,
//   },
// })

// 2. ts 的方式
// 结构给默认值的方式, 做好了响应式 => 实验性语法, 需要额外配置
const {
  slides,
  autoPlay = false,
  duration = 2000,
} = defineProps<{
  slides: BannerItem[]
  autoPlay?: boolean //autoPlay 控制是否自动轮播, 默认false 不自动轮播
  duration?: number // 控制切换时间, 隔几秒切换图片  默认是2s后切换
}>()

// 准备一个数据, 记录激活的轮播图下标
const active = ref(0)

// 左右箭头切换轮播图图片
const prev = () => {
  // 如果激活项的下标, 到了0, 回到最大值 length-1 反之 --
  if (active.value === 0) {
    active.value = slides.length - 1
  } else {
    active.value--
  }
}

const next = () => {
  // 如果激活项的下标, 到了最大值, 回到0 反之 ++
  if (active.value === slides.length - 1) {
    active.value = 0
  } else {
    active.value++
  }
}

// 优化自动轮播: 鼠标移入banner区域 停止轮播; 移除区域, 继续轮播
// 自动轮播 本质 就还是开一个定时器 不停的执行next

const play = () => {
  if (!autoPlay) return
  // 模块化环境会将定时器当成node, 其实是浏览器中的, 需要显示的指定
  timer = window.setInterval(() => {
    next()
    console.log(11111)
  }, duration)
}

const stop = () => {
  clearInterval(timer)
}
let timer: number = -1
onMounted(() => {
  // 轮播图dom准备就绪.. 可以开启自动轮播了
  play()
})

onUnmounted(() => {
  stop()
})
</script>

<template>
  <div class="xtx-carousel" @mouseenter="stop" @mouseleave="play">
    <!-- 轮播图主题 -->
    <ul class="carousel-body">
      <li
        v-for="(item, index) in slides"
        :key="item.id"
        class="carousel-item"
        :class="{ fade: active === index }"
      >
        <RouterLink to="/">
          <img :src="item.imgUrl" alt="" />
        </RouterLink>
      </li>
    </ul>

    <!-- 左箭头 -->
    <a href="javascript:;" @click="prev" class="carousel-btn prev"
      ><i class="iconfont icon-angle-left"></i
    ></a>
    <!-- 右箭头 -->
    <a href="javascript:;" @click="next" class="carousel-btn next"
      ><i class="iconfont icon-angle-right"></i
    ></a>
    <!-- 原点指示器 -->
    <div class="carousel-indicator">
      <span
        :class="{ active: active === index }"
        v-for="(item, index) in slides"
        :key="item.id"
        @mouseenter="active = index"
      ></span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-carousel {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>
